//
//  eLiveRadioLiveRadioVController.m
//  find
//
//  Created by 吕超 on 16/12/21.
//  Copyright © 2016年 ywydz. All rights reserved.
//

#import <TXRTMPSDK/TXLivePush.h>
#import <TXRTMPSDK/TXLivePlayer.h>
#import <TXRTMPSDK/TXLivePlayListener.h>
#import <mach/mach.h>
#import "eLiveRadioLiveRadioVController.h"
#import "AppDelegate.h"
#import "Tools.h"
// 设置颜色
#define UIColorRGB(x,y,z) [UIColor colorWithRed:x/255.0 green:y/255.0 blue:z/255.0 alpha:1.0]
#define selfWidth [UIScreen mainScreen].bounds.size.width
#define selfHeigh [UIScreen mainScreen].bounds.size.height

@interface eLiveRadioLiveRadioVController ()<UIScrollViewDelegate,UITextViewDelegate>
{
    TXLivePlayer *_txLivePlayer;
    TX_Enum_PlayType _playType;
    TXLivePush * _txLivePublisher;
}


@property (nonatomic, strong)UIView *backView;

@property (nonatomic, strong)UIView *placeholderView;

// 横屏 btn
@property (nonatomic, strong)UIButton *hengPingBtn;
// 播放 暂停
@property (nonatomic, strong)UIButton *PlayBtn;

@property (nonatomic, assign)NSInteger placeholderHeight;

@end



@implementation eLiveRadioLiveRadioVController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];
    
    //将原点移动到navigationBar
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    self.navigationController.navigationBar.barTintColor = [UIColor redColor];
    
    // 16 : 9
    _placeholderHeight = selfWidth / 16 * 9;
    
    //设置占位背景
    self.placeholderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, selfWidth ,_placeholderHeight)];
    
    _placeholderView.backgroundColor = [UIColor blackColor];
    
    [self.view addSubview:self.placeholderView];
    
    self.backView = [[UIView alloc]initWithFrame:CGRectMake(0, _placeholderHeight, selfWidth, 35)];
    
    [self.view addSubview:_backView];
    
    self.hengPingBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    _hengPingBtn.frame = CGRectMake(selfWidth - 40 , _placeholderHeight - 40, 40, 40);
    
    [_hengPingBtn setImage:[UIImage imageNamed:@"fullscreen_icon"] forState:(UIControlStateNormal)];
    
    _hengPingBtn.selected = YES;
    
    [_hengPingBtn addTarget:self action:@selector(hengPingButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.placeholderView addSubview:self.hengPingBtn];
    
    
    //    PlayBtn
    self.PlayBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  
    _PlayBtn.frame = CGRectMake(0 , _placeholderHeight - 40, 40, 40);

    [_PlayBtn setImage:[UIImage imageNamed:@"stop_icon"] forState:(UIControlStateNormal)];
    
    _PlayBtn.selected = YES;
    
    [_PlayBtn addTarget:self action:@selector(PlayBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.placeholderView addSubview:self.PlayBtn];
    
    
   _txLivePlayer = [[TXLivePlayer alloc] init];
    
    [_txLivePlayer setupVideoWidget:CGRectMake(0, 0, 0, 0) containView:self.placeholderView insertIndex:0];
    
    _txLivePlayer.enableHWAcceleration = YES;
    
    TXLivePlayConfig  *_config = [[TXLivePlayConfig alloc] init];
    
    //流畅模式
    _config.bAutoAdjustCacheTime   = NO;
    // 播放器缓存时间
    _config.cacheTime  = 3;
    
    [_txLivePlayer setConfig:_config];
    // 全屏
    [_txLivePlayer setRenderMode:RENDER_MODE_FILL_SCREEN];
    
     [_txLivePlayer setRenderRotation:HOME_ORIENTATION_DOWN];
    
    //  播放                  播放地址
    [_txLivePlayer startPlay:@"rtmp://live.hkstv.hk.lxdns.com/live/hks" type:_playType];
}






#pragma mark  ====   全屏点击事件
- (void)hengPingButtonAction:(UIButton *)sender{
    
    if (sender.selected == YES){
        
        [self.view bringSubviewToFront:self.placeholderView];
        
        [Tools orientationToPortrait:UIInterfaceOrientationLandscapeRight];
        
        self.placeholderView.frame = [UIScreen mainScreen].bounds;
        
        _hengPingBtn.frame = CGRectMake(selfWidth - 40 , selfHeigh - 40, 40, 40);
        
        [_hengPingBtn setImage:[UIImage imageNamed:@"exitfullscreen_icon"] forState:(UIControlStateNormal)];
        
        [_PlayBtn setFrame:CGRectMake(0, selfHeigh - 40 , 40, 40)];
        
        self.navigationController.navigationBarHidden = YES;
        
        sender.selected = NO;
        
    } else {
        
        [Tools orientationToPortrait:UIInterfaceOrientationPortrait];
        
        [_hengPingBtn setImage:[UIImage imageNamed:@"fullscreen_icon"] forState:(UIControlStateNormal)];
        
        self.placeholderView.frame = CGRectMake(0, 0, selfWidth  , _placeholderHeight);
        
        _hengPingBtn.frame = CGRectMake(selfWidth - 40 , _placeholderHeight - 40, 40, 40);
        
        [_PlayBtn setFrame:CGRectMake(0, _placeholderHeight - 40, 40, 40)];
        
        self.navigationController.navigationBarHidden = NO;
        
        sender.selected = YES;

    }

}




#pragma mark  ====  播放  暂停  事件
- (void)PlayBtnAction:(UIButton  *)btn{
    if (btn.selected == YES){
         [btn setImage:[UIImage imageNamed:@"play_icon"] forState:(UIControlStateNormal)];
        // 暂停
        [_txLivePlayer pause];
       
        
        btn.selected = NO;
    } else {
        [btn setImage:[UIImage imageNamed:@"stop_icon"] forState:(UIControlStateNormal)];
        // 恢复
        [_txLivePlayer resume];
        
        btn.selected = YES;
    }
}





- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

    AppDelegate * delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    
    delegate.allowRotate = 1;
}


- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    
    AppDelegate * delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    delegate.allowRotate = 0;

}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
